package com.rfy.novel.controller;

import com.alibaba.fastjson.JSONObject;

import com.rfy.base.entity.response.BaseResponse;
import com.rfy.framework.util.BeanUtil;
import com.rfy.framework.util.HttpUtil;
import com.rfy.novel.api.NovelApi;
import com.rfy.novel.chain.ReadChain;
import com.rfy.novel.chain.client.SearchWebsite;
import com.rfy.novel.entity.Book;
import com.rfy.novel.entity.BookCatalogue;
import com.rfy.novel.request.ReadNovelContentRequest;
import com.rfy.novel.request.WebsiteRequest;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

import static com.rfy.base.constant.GlobalConstants.SUCCESS;


/**
 * @author zhanghuiyong
 * @创建时间 2019年07月16日
 * @描述: 小说阅读模块
 **/

@RestController
@CrossOrigin
public class NovelReadRestController implements NovelApi {


    @Override
    @ResponseBody
    public BaseResponse queryNovel(@RequestBody WebsiteRequest novelRequestVo, HttpServletResponse response) {
        BaseResponse bookBaseResponse = new BaseResponse<>();
        System.out.println("开始搜索小说:" + novelRequestVo.getBookName());
        Book novel = null;
        try {
            //解决跨域请求问题
            HttpUtil.domainCross(response);
            ReadChain readChain = new ReadChain();
            SearchWebsite website = (SearchWebsite) readChain.execute(novelRequestVo.getWebSiteName());
            novel = website.getNovel(novelRequestVo.getBookName());
            bookBaseResponse.setStatus(SUCCESS);
            bookBaseResponse.setResult(novel);
            System.out.println("小说检索查看返回值," + JSONObject.toJSONString(bookBaseResponse));
        } catch (Exception e) {
            e.printStackTrace();
            bookBaseResponse.setMessage("小说检索出错" + e.getMessage());
        }
        return bookBaseResponse;
    }

    @Override
    @ResponseBody
    public BaseResponse readNovelContent(@RequestBody ReadNovelContentRequest novelRequestVo, HttpServletResponse response) {
        BaseResponse bookBaseResponse = new BaseResponse<>();
        System.out.println("开始阅读小说:" + novelRequestVo.getTitle());
        try {
            //解决跨域请求问题
            HttpUtil.domainCross(response);
            ReadChain readChain = new ReadChain();
            SearchWebsite website = (SearchWebsite) readChain.execute(novelRequestVo.getWebSiteName());

            BookCatalogue bookCatalogue = getBookCatalogue(novelRequestVo);
            bookCatalogue = website.getNovelContent(bookCatalogue);
            bookBaseResponse.setStatus(SUCCESS);
            bookBaseResponse.setResult(bookCatalogue);
            System.out.println("阅读小说内容返回值," + JSONObject.toJSONString(bookBaseResponse));
        } catch (Exception e) {
            e.printStackTrace();
            bookBaseResponse.setMessage("小说阅读出错" + e.getMessage());
        }
        return bookBaseResponse;
    }

    private BookCatalogue getBookCatalogue(ReadNovelContentRequest novelRequestVo) {
        BookCatalogue bookCatalogue = new BookCatalogue();
        BeanUtil.copyObject(novelRequestVo, bookCatalogue);
        return bookCatalogue;
    }

}
